import AddressService from "../../../class/service/AddressService";
import Router from "../../../class/Router";
import Tips from "../../../utils/Tips";

const notification = require("../../../utils/WxNotificationCenter.js");
const addressService = new AddressService();
const util = require("../../../utils/util.js")
const storage = require("../../../utils/storage.js")
const constant = require("../../../constant.js")

Page({
  page: {},
  data: {
    mode: '',
    addresses: [],
    init: true,
  },

  onLoad: function (options) {
    const mode = options.mode;
    if (mode) {
      this.setData({ mode: mode });
    }
    if(options.selectId) {
      console.log(options.selectId)
      this.setData({ selectId: options.selectId})
    }
    var unionid = wx.getStorageSync(storage.keys.unionid)
    this.setData({ unionid: unionid})
  },

  onShow: function (options) {
    var that = this;
    Tips.loading();
    that.list();
  },

  /**
  * 加载下一页
  */
  list: function () {
    var that = this;
    addressService.list(that.data.unionid).then(data => {
      that.initAddress(data.userAddresses)
      Tips.loaded();
    });
  },

  initAddress: function (userAddresse) {
    var that = this;
    var addresses = [];
    for (var i in userAddresse) {
      var address = {};
      address['id'] = userAddresse[i].id;
      address['name'] = userAddresse[i].reciver;
      address['phone'] = userAddresse[i].reciver_mobile;
      address['province'] = userAddresse[i].province;
      address['city'] = userAddresse[i].city;
      address['country'] = userAddresse[i].district;
      address['detail'] = userAddresse[i].address;
      address['fullAddress'] = userAddresse[i].province + userAddresse[i].city + userAddresse[i].district + userAddresse[i].address;
      console.log(userAddresse[i].id == that.data.selectId)
      address['isDefault'] = userAddresse[i].id == that.data.selectId //userAddresse[i].selected
      addresses.push(address);
    }
    console.log(addresses)
    that.setData({
      addresses: addresses
    })
  },

  /**
   * 从微信新增
   */
  onWxAddressTap: function () {

    var that = this;

    wx.chooseAddress({
      success: (res) => {
        that.setData({
          addressInfo: res
        })
      },
      fail: function (err) {
        console.log(err)
      }
    })
    
    addressService.wxAddress()
      .then(res => {
        const message = `确定增加：${res.name},${res.phone},${res.province}${res.city}${res.country},${res.detail}作为收货地址?`;
        return Tips.confirm(message, res);
      })
      .then(add => {
        addressService.save(that.data.unionid, add).then(res => {
          Tips.toast('地址新增成功', () => that.list());
        });
      })
      
  },

  /**
   * 点击默认
   */
  onDefaultTap: function (event) {
    const addrId = event.currentTarget.dataset.id;
    const isAreadyDefault = this.data.addresses.find(addr => addr.id == addrId).isDefault;
    if (isAreadyDefault == 1) {
      return;
    }

    Tips.loading();
    addressService.setDefault(this.data.unionid, addrId).then(res => {
      Tips.toast('设置成功', () => this.list());
    });
  },

  /**
   * 删除地址
   */
  onDeleteTap: function (event) {
    const addrId = event.currentTarget.dataset.id;
    Tips.confirm('是否确认删除该地址', addrId)
      .then(res => {
        Tips.loading();
        return addressService.remove(addrId);
      })
      .then(res => {
        Tips.toast('删除成功', () => this.list());
      });
  },

  /**
   * 编辑地址
   */
  onEditTap: function (event) {
    const addrId = event.currentTarget.dataset.id;
    const result = this.data.addresses.filter(addr => addr.id == addrId);
    if (result.length > 0) {
      const addr = JSON.stringify(result[0]);
      Router.addressEdit(addr);
    }
  },

  /**
   * 点击地址
   */

  onAddressTap: function (event) {
    const addrId = event.currentTarget.dataset.id;
    //只响应选择模式的点击
    if (this.data.mode != 'choice') {
      return;
    }

    const choice = this.data.addresses.find(addr => addr.id == addrId);
    notification.postNotificationName("ON_ADDRESS_CHOICE", choice);
    Router.back();
  }
});